 package ru.spb.osll.myfit.client.view;

import com.google.gwt.user.client.ui.Widget;
import ru.spb.osll.myfit.client.Mode;
import ru.spb.osll.myfit.client.event.PageButtonEvent;
import ru.spb.osll.myfit.client.event.PageEvent;
import ru.spb.osll.myfit.client.event.PageEvent.PageEventType;
import ru.spb.osll.myfit.client.event.TagAddEvent;
import ru.spb.osll.myfit.client.widget.PageButtonEventListener;
import ru.spb.osll.myfit.client.widget.PageWidget;
import ru.spb.osll.myfit.common.Page;

public class MyFitView extends AbstractView {

  private Mode m_mode = Mode.VIEW;

  private final PageWidget m_pageWidget;

  private PageButtonEventListener m_internalButtonListener;
  
  private MyFitToolBar m_toolBar;
 
  public MyFitView(PageWidget pageWidget, PageButtonEventListener pageButtonEventListener) {
    m_internalButtonListener = createInternalButtonEventListener(pageButtonEventListener);
    m_pageWidget = pageWidget;
    m_pageWidget.setMode(m_mode);
  }

  private PageButtonEventListener createInternalButtonEventListener(final PageButtonEventListener externalButtonListener) {
    return new PageButtonEventListener() {

      @Override
      public void actionPerformed(PageButtonEvent event) {
        switch (event.getType()) {
        case ADD_TABLE:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_TABLE));
          break;
        case ADD_INCLUDE:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_INCLUDE));
          break;
        case ADD_WIKI:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_WIKI));
          break;
        case ADD_CONTENT:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_CONTENT));
          break;
        case ADD_DEFINE:
          m_pageWidget.actionPerformed(new PageEvent(PageEventType.ADD_DEFINE));
          break;
        case COLLAPSE_ALL:
          m_pageWidget.setOpen(false);
          break;
        case EXPAND_ALL:
          m_pageWidget.setOpen(true);
          break;
        case TO_EDIT_MODE:
          changeMode(Mode.EDIT);
          break;
        case TO_VIEW_MODE:
          changeMode(Mode.VIEW);
          break;
        case ADD_TAG:
          if (event instanceof TagAddEvent) {
            ((TagAddEvent) event).setAlreadyExists(!m_pageWidget.addTag(((TagAddEvent) event).getTagName()));
          }
          break;
        default:
          externalButtonListener.actionPerformed(event);
          break;
        }
      }
    };
  }

  @Override
  protected Widget createEditPanel() {   
    m_toolBar = new MyFitToolBar(m_internalButtonListener, m_pageWidget.getPagePath());
    return m_toolBar; 
  }

  @Override
  protected Widget createContentPanel() {
    return m_pageWidget;
  }

  public void reloadPage(Page page) {
    m_pageWidget.clear();
    m_pageWidget.setPage(page);
  }

  public void setVerifierResult(boolean hasError) {
    m_toolBar.setVerificationResult(hasError);
  }

  public Page getPage() {
    return m_pageWidget.getPage();
  }

  public String getPagePath(){
    return m_pageWidget.getPagePath();
  }
  public void reloadTemplates() {
    m_pageWidget.actionPerformed(new PageEvent(PageEventType.RELOAD_TEMPLATES)); 
  }
  
  private void changeMode(Mode newMode){
    m_mode = newMode;
    m_pageWidget.setMode(m_mode);
    m_toolBar.setMode(m_mode);
  }
}
